package com.fight.strive.sys.modules.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fight.strive.sys.modules.exception.StriveException;
import com.fight.strive.sys.modules.rbac.dao.RbacUserRoleMapper;
import com.fight.strive.sys.modules.rbac.entity.RbacUserRoleEntity;
import com.fight.strive.sys.modules.rbac.service.RbacUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class RbacUserRoleServiceImpl
        extends ServiceImpl<RbacUserRoleMapper, RbacUserRoleEntity>
        implements RbacUserRoleService {

    @Override
    public void saveUserRole(RbacUserRoleEntity entity) {
        try {
            this.saveOrUpdate(entity);
        } catch (DuplicateKeyException e) {
            throw new StriveException("用户角色重复关联");
        }
    }

    @Override
    public void removeByRoleId(Long roleId) {
        QueryWrapper<RbacUserRoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        this.remove(queryWrapper);
    }

    @Override
    public void removeUserId(Long userId) {
        QueryWrapper<RbacUserRoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        this.remove(queryWrapper);
    }
}
